package org.xiaom.butler.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xiaom.butler.util.Standard;
import org.xiaom.butler.util.Util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

public class FileServlet extends HttpServlet {
	private String field = "file";

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String url = request.getRequestURI();
		String function = url.split("\\.")[1];
		String name = url.split("/" + field + "/")[1];
		if ("do".equals(function)) { // 请求是个功能性请求
			if ("upload.do".equals(name)) {
				upLoad(request, response);
			}else if("delete.do".equals(name)){
				delete(request,response);
			}
		} else if ("get".equals(function)) {
			if ("download.get".equals(name)) {
				downLoad(request, response);
			} else if ("path.get".equals(name)) {
				getPath(request, response);
			}
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		
	}

	private void getPath(HttpServletRequest request,
			HttpServletResponse response) {
		String path = getServletContext().getRealPath("/")+ Standard.RESOURCE_PATH_IMAGE;
		String name = Util.getQueryParam(request, "name", "");
		String state="success";
		if(Util.isBlank(name)){
			state="failed";
		}
		name = path + "/" + name;
		GsonBuilder gb=new GsonBuilder();
		JsonObject jo=new JsonObject();
		jo.addProperty("state", state);
		jo.addProperty("path", name);
		response.setContentType("text/json");
		try {
			response.getWriter().write(jo.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void downLoad(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String path = getServletContext().getRealPath("/")
				+ Standard.RESOURCE_PATH_IMAGE;
		String name = Util.getQueryParam(request, "name", "");
		name = "1389171554454.gif";
		name = path + "/" + name;
		Util.downLoad(request, response, name, "");
	}

	protected void upLoad(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String rs;
		String type_path="";
		String type=Util.getQueryParam(request, "file_type","image");
		if("image".equalsIgnoreCase(type)){
			type_path= Standard.RESOURCE_PATH_IMAGE;
		} else if("app_android".equalsIgnoreCase(type)){
			type_path= Standard.RESOURCE_PATH_APP_ANDROID;
		}
		try {
			String prefix = getServletContext().getRealPath("/")
					+ type_path;
			String imagePath[] = Util.upLoadFile(request, response, prefix);
			rs = "{'" + "path':'" + getServletContext().getContextPath()
					+ type_path + "/" + imagePath[0]
					+ "','state':'success','beanPath':'"
					+ type_path + "/" + imagePath[0] + "'}";
		} catch (Exception t) {
			t.printStackTrace();
			rs = "{'state':'failed'}";
		}
		System.out.println(rs);
		response.getWriter().write(rs);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
